package org.sgx.gwteditors.client.impl1.simple;

import java.util.List;

import org.sgx.gwteditors.client.editor.Editor;
import org.sgx.gwteditors.client.editor.error.AbstractEditorError;
import org.sgx.gwteditors.client.editor.error.EditorError;
import org.sgx.gwteditors.client.editor.error.EditorErrorManager;
import org.sgx.gwteditors.client.editor.event.EditorEventManager;
import org.sgx.gwteditors.client.editor.event.EditorValueChangeEvent;
import org.sgx.gwteditors.client.editor.event.ValueChangeListener;
import org.sgx.gwteditors.client.impl1.EditorFramework1;
import org.sgx.gwteditors.client.impl1.EditorManager1;
import org.sgx.gwteditors.client.impl1.ui.sbutton.SButton;
import org.sgx.gwteditors.client.impl1.ui.sbutton.SButtonListener;

import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.Widget;

/**
 * an integer editor that is an entry with increment/decrement buttons. 
 * if those buttons are keep pressed the counter will keep counting.
 * @author sg
 *
 */
public class IntegerEditorEntry2  extends FlexTable implements Editor<Integer>, ValueChangeHandler<Integer> {

	private IntegerBox integerBox;

	public IntegerEditorEntry2() {
		super(); 
		
		integerBox = new IntegerBox();
		setWidget(0, 0, integerBox);
		integerBox.setWidth("100%");
		getCellFormatter().setVerticalAlignment(0, 0, HasVerticalAlignment.ALIGN_TOP);
		integerBox.addValueChangeHandler(this); 
		FlexTable flexTable = new FlexTable();
		setWidget(0, 1, flexTable);
		
		SButton btnNewButton = new SButton("+");
		flexTable.setWidget(0, 0, btnNewButton);
		btnNewButton.addSButtonListener(new SButtonListener() {			
			@Override
			public void notifyStateChanged(boolean pressed) {
				if(pressed) {
					integerBox.setText(""+(Integer.parseInt(integerBox.getText())+1));
					notifyEditorValueChange();
				}
			}
		}); 
		
		SButton btnNewButton_1 = new SButton("-");
		flexTable.setWidget(1, 0, btnNewButton_1);
		getCellFormatter().setVerticalAlignment(0, 1, HasVerticalAlignment.ALIGN_TOP);
		getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_LEFT);
		btnNewButton_1.addSButtonListener(new SButtonListener() {			
			@Override
			public void notifyStateChanged(boolean pressed) {
				if(pressed) {
					integerBox.setText(""+(Integer.parseInt(integerBox.getText())-1));
					notifyEditorValueChange();
				}
			}
		}); 
		
	}
	@Override
	public void addValueChangeListener(ValueChangeListener<Integer> l) {
		EditorEventManager.<Integer>getInstance().addValueChangeListener(this, l);
	}

	@Override
	public void removeValueChangeListener(ValueChangeListener<Integer> l) {
		EditorEventManager.<Integer>getInstance().removeValueChangeListener(this, l);
	}

	@Override
	public Integer flush() {
		try {
			return Integer.parseInt(integerBox.getText());
		} catch (Exception e) {
			EditorErrorManager.<Integer>getInstance().setError(this, 
				new AbstractEditorError<Integer>(integerBox.getText()+ 
				" is not a valid integer ", this));
			return null;
		}
	}

	@Override
	public List<EditorError<Integer>> getErrors() {
		return EditorErrorManager.<Integer>getInstance().getErrors(this);
	}

	@Override
	public void load(Integer model) {
		integerBox.setText(model+""); 
	}

	@Override
	public Widget getWidget() {
		return this; 
	}

	@Override
	public boolean isSimple() {
		return true;
	}
	@Override
	public void onValueChange(ValueChangeEvent<Integer> event) {
		notifyEditorValueChange(); 
	}
	private void notifyEditorValueChange() {
		EditorValueChangeEvent<Integer> ev_ = new EditorValueChangeEvent<Integer>(this, flush(), null);		
		EditorEventManager.<Integer>getInstance().fireValueChangeEvent(this, ev_);	
	}
	@Override
	public String getEditorId() {
		return EditorManager1.getInstance().getEditorName(this);		
	}

	@Override
	public String[] getEditorTypesSupported() {
		return new String[]{EditorFramework1.TYPE_INTEGER}; 
	}
}
